//
//  T3FAnnotation.swift
//  t3_map
//
//  Created by Cunhai Li on 2024/10/22.
//

import CoreLocation
import AMapNaviKit
import Bundle_Log
import t3_assets

enum T3FAnnotationViewStyle: String {
    case pin
    case bubble
    case wait
}

struct T3FAnnotationOptions {
    /// 唯一标识符
    let identifier: String
    /// 经纬度
    var coordinate: CLLocationCoordinate2D

    /// 标注物样式，决定使用哪个view来创建annotationView
    var style: T3FAnnotationViewStyle

    var zIndex: Int

    var args: [String: Any]?

    init(coordinate: CLLocationCoordinate2D, 
         identifier: String,
         style: T3FAnnotationViewStyle,
         zIndex: Int,
         args: [String: Any]?) {
        self.identifier = identifier
        self.coordinate = coordinate
        self.style = style
        self.zIndex = zIndex
        self.args = args
    }
    
    init?(from jsonString: String) {
        do {
            let json = try T3FJsonUtil.toJson(jsonString)
            self.init(from: json)
        } catch (let error) {
            T3Log.error("annotation初始化异常，error = \(error.localizedDescription)")
            return nil
        }
    }

    init?(from json: [String: Any]) {
        guard let uid = json["uid"] as? String else { return nil }
        guard let longitude = json["longitude"] as? Double else { return nil }
        guard let latitude = json["latitude"] as? Double else { return nil }
        guard let styleValue = json["style"] as? String else { return nil }

        let zIndex = json["zIndex"] as? Int ?? 0
        let args = json["args"] as? [String: Any]

        guard let style = T3FAnnotationViewStyle(rawValue: styleValue) else { return nil }

        self.identifier = uid
        self.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        self.style = style
        self.zIndex = zIndex
        self.args = args
    }
}

protocol T3FAnnotationView: UIView {
    func update(argvs: [String: Any]?)

    /// 响应点击事件
    /// note: 参数及类型是临时预留，暂时未使用，后续有具体使用场景时，可考虑调整参数类型
    var annotationDidClicked: (([String: Any]?) -> Void)? { get set }
}

extension T3FAnnotationView {
    func update(argvs: [String: Any]?) { }
}

// MARK: - T3Annotation
protocol T3FAnnotation: NSObject {
    
    /// 参数配置
    var options: T3FAnnotationOptions { get }

    var annotationView: T3FAnnotationView { get set }
}

extension T3FAnnotation {
    func createAnnotationView() -> T3FAnnotationView {
        switch options.style {
        case .pin:
            if let args = self.options.args {
                if let pinOptions = T3FPinAnnotationView.Options(json: args) {
                    return T3FPinAnnotationView(options: pinOptions)
                }
            }
        case .bubble:
            if let args = self.options.args {
                if let bubbleOptions = T3FBubbleAnnotationView.Options(json: args) {
                    return T3FBubbleAnnotationView(options: bubbleOptions)
                }
            }
        case .wait:
            if let args = self.options.args {
                if let waitOptions = T3FWaitAnnotationView.Options(json: args) {
                    return T3FWaitAnnotationView(options: waitOptions)
                }
            }
        }

        assertionFailure("annotationView构建失败")
        return T3FAnnotationEmptyView()
    }
}

class T3FAnnotationEmptyView: UIView, T3FAnnotationView {
    var annotationDidClicked: (([String : Any]?) -> Void)?
}
